/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/8/19 14:44
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.model;

import com.spring.arch.common.enums.DataType;
import com.spring.arch.common.jpa.model.AbstractLogicalModel;
import com.spring.arch.uaa.enums.ControlRange;
import com.spring.arch.uaa.enums.ModuleType;
import com.spring.arch.common.enums.DataType;
import com.spring.arch.common.jpa.model.AbstractLogicalModel;
import com.spring.arch.uaa.enums.ControlRange;
import com.spring.arch.uaa.enums.ModuleType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色
 *
 * @author Frank
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "role")
public class Role extends AbstractLogicalModel implements GrantedAuthority {

    private static final long serialVersionUID = 367897661055096321L;

    /**
     * 角色编码
     */
    @NotBlank
    @Size(max = 32)
    @Column(nullable = false, unique = true)
    private String code;

    /**
     * 租户（租户自定义角色时使用）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    private Tenant tenant;

    /**
     * 角色名称
     */
    @NotBlank
    @Size(max = 64)
    @Column(nullable = false, unique = true)
    private String name;

    /**
     * 数据类型
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private DataType dataType;

    /**
     * 管控范围: 集团级、项目级
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private ControlRange controlRange;

    /**
     * 模块类型
     */
    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private ModuleType moduleType;

    /**
     * 自定义角色由哪些内置角色构成
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "role_origins", joinColumns = @JoinColumn(name = "custom_role_id"),
            inverseJoinColumns = @JoinColumn(name = "internal_role_id"))
    private List<Role> internalRoles = new ArrayList<>();

    public Role(String code, String name, DataType dataType, ControlRange controlRange, ModuleType moduleType) {
        this.code = code;
        this.name = name;
        this.dataType = dataType;
        this.controlRange = controlRange;
        this.moduleType = moduleType;
    }

    @Override
    public String getAuthority() {
        return this.code;
    }
}
